﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using appladen.contracts;

namespace appladen.cache
{
    public class CacheDisposer : appladen.contracts.ICache
    {
        private IFactory _independent;

        internal readonly Dictionary<long, IDetailviewBoard> _cache = new Dictionary<long, IDetailviewBoard>();

        public void Inject(IFactory independent)
        {
            _independent = independent;
        }

        public void Process(long message)
        {
            Trace.WriteLine("Cache disposer");
            if (!_cache.ContainsKey(message))
            {                
                _cache.Add(message, _independent.Create());                
            }
            Result(_cache[message]);
        }

        public void Close(IDetailviewBoard message)
        {
            var element = _cache.First(x => x.Value == message).Key;
            _cache.Remove(element);
        }

        public event Action<IDetailviewBoard> Result;
    }
}
